const Base = require('../rest.js');
module.exports = class extends Base
{

    constructor(ctx)
    {
        super(ctx);
        this._model = this.model('apiv1/application');
        this.appModel = this._model;
    }

    async uploadWorkAction()
    {
        let work_step_data = {};

        //作品表数据
        let data = this.ctx.state.uploadWorkData;
        data.PK_WorksGuid = think.uuid('v3');
        data.PK_CompetitionStepsGuid = data.stepguid;
        data.FK_UserGuid = data.userguid;
        data.IsDelete = '未删除';
        data.CreateTime = think.nowTimestamp();

        //中间表数据
        work_step_data.PK_WorksNoGuid = think.uuid('v3');
        work_step_data.FK_WorksGuid = data.PK_WorksGuid;
        work_step_data.FK_CompetitionStepsGuid = data.PK_CompetitionStepsGuid;
        let addInfo = await this._model.addWorks(data, work_step_data);
        this.error(addInfo);

    }

    /**
     * put resource
     * @return {Promise} []
     */
    async postAction()
    {
        let cpguid = this.ctx.state.cpguid;
        if (think.isObject(this.ctx.state.psrData))
        {
            let saveData = this.ctx.state.psrData;
            saveData.PK_ApplicationGuid = think.uuid('v3');
            saveData.FK_CompetitionGuid = cpguid;
            saveData.FK_UserGuid = saveData.userguid;
            saveData.CreateTime = think.nowTimestamp();
            let res = await this._model.add(saveData);
            if (res !== false)
            {
                return this.error(0, '添加成功');
            } else
            {
                return this.error(-1, '系统繁忙,请稍后再试')
            }
        }

        if (think.isObject(this.ctx.state.teamData))
        {
            let saveData = this.ctx.state.teamData;
            saveData.PK_ApplicationGuid = think.uuid('v3');
            saveData.FK_CompetitionGuid = cpguid;
            saveData.FK_UserGuid = saveData.userguid;
            saveData.CreateTime = think.nowTimestamp();
            //生成队员
            let menmberDate = this._getMenmberData(saveData.Menmber, saveData.PK_ApplicationGuid);
            let res = await this._model.add(saveData);
            let res1 = await this.model('apiv1/member').addMany(menmberDate);
            if (res !== false && res1 !== false)
            {
                return this.error(0, '添加成功');
            } else
            {
                return this.error(-1, '系统繁忙,请稍后再试')
            }
        }

    }

    _getMenmberData(data, applicationGuid)
    {
        let res = [];
        for (let index in data)
        {
            let temp = {};
            temp.PK_MemberGuid = think.uuid('v3');
            temp.FK_ApplicationGuid = applicationGuid;
            temp.Name = data[index];
            res.push(temp);
        }
        return res;
    }


    /**
     * 是否已经报名
     */
    async isapplyAction()
    {
        let postData = think.stringToJson(this.post('data'));
        if (postData === false)
        {
            return this.fail(52004, '提交的表单信息错误,该数据必须为json格式,请检查数据格式,上传数据格式为{\'data\',yourJsonData},如 : $.post("http://127.0.0.1:8091/apiv1/competition",{\'data\':yourJsonData}),yourJsonData为你要上传的数据包');
        }
        if (postData.cpguid && postData.userguid)
        {
            let res = await this.appModel.where({
                FK_CompetitionGuid: postData.cpguid,
                FK_UserGuid: postData.usrguid
            }).find();
            return this.responseQuery(res);
        } else
        {
            return this.responseEmpty();
        }


    }

};
